<template>
  <a-modal :visible="visible" :title="form.id?'添加周报信息':'修改周报信息'" :width="700" forceRender ok-text="确认" cancel-text="取消" @ok="onSubmit" @cancel="onClose">
    <a-form ref="formRef" :model="form" :rules="rules" >
      <a-form-item label="周报日期" name="weekDate">
        <a-date-picker v-model:value="releaseTime" @change="changeTime" :format="timeFormat" placeholder="请输入周报日期" style="width: 100%" />
      </a-form-item>
       <a-form-item label="项目进度(%)" name="presentRate">
        <a-input-number v-model:value="form.presentRate" :min="0" :max="100" placeholder="请输入任务进度，无小数位" style="width: 100%" />
        </a-form-item>
        <a-form-item label="周报内容" name="content">
          <a-textarea v-model:value="form.content" placeholder="请输入周报内容" :rows="3" />
        </a-form-item>
      <a-form-item label="项目风险" name="remark">
        <a-textarea v-model:value="form.riskDesc" placeholder="请输入项目风险" :rows="3" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
  import { message } from 'ant-design-vue';
  import _, { fromPairs } from 'lodash';
  import { nextTick, reactive, ref } from 'vue';
  import dayjs from 'dayjs';
  import { projectApi } from '/@/api/task/project-api';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { smartSentry } from '/@/lib/smart-sentry';

  defineExpose({
    showModal,
  });
  const emit = defineEmits('refresh');

  // --------------------- modal 显示与隐藏 ---------------------
  // 是否展示
  const visible = ref(false);
  const timeFormat = 'YYYY-MM-DD';
  const releaseTime = ref(null);

  function showModal(record) {
    if(record){
      Object.assign(form, record);
      if(form.weekDate)
        releaseTime.value = dayjs(form.weekDate);
      else
        releaseTime.value = null;
    }      
    visible.value = true;
  }

  function changeTime(date, dateString) {
    form.weekDate = dateString;
  }

  function onClose() {
    emit('refresh');
    visible.value = false;
  }

  // --------------------- 表单 ---------------------

  //  组件
  const formRef = ref();

  const formDefault = {
    id: undefined,
    weekDate: undefined,
    content: undefined,
    projectId: undefined,
    presentRate: undefined,
    riskDesc:undefined,
  };
  let form = reactive({ ...formDefault });
  const rules = {
    weekDate: [{ required: true, message: '请输入周报日期' }],
    content: [{ required: true, message: '请输入周报内容' }],
  };

  function onSubmit() {
    formRef.value
      .validate()
      .then(async () => {
        SmartLoading.show();
        try {
          let res=await projectApi.saveDetail(form);
          message.success(res.message);
          onClose();
        } catch (error) {
          smartSentry.captureError(error);
        } finally {
          SmartLoading.hide();
        }
      })
      .catch((error) => {
        console.log('error', error);
        message.error('参数验证错误，请仔细填写表单数据!');
      });
  }
</script>

<style lang="less" scoped>
  .form-width {
    width: 100% !important;
  }

  .footer {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }

  :deep(.ant-card-body) {
    padding: 10px;
  }
</style>
